package mainpkg

import (
    "io"
    "appengine"
//    "template"
    "fmt"
	"bytes"
	//mail "gae/mail"
	gae_mail "appengine/mail"
)



/*
var _ = mime.ParseMediaType
var _ = mail.Message{}
var _ = bufio.NewReader
var _ = multipart.NewReader
var _ = textproto.NewReader
*/


func MailEcho( c appengine.Context, is io.Reader) {
	c.Debugf( "mailEcho: Enter")
	
	var bufferOut bytes.Buffer
    //fOut := bytes.NewBuffer( bufferOut)
	//bufferOut2 := make([]byte, 40*1024)[:0]
    //fOut2 := bytes.NewBuffer( bufferOut2)
	var bufferOut2 bytes.Buffer

	//is := myMail.NewReader()
	var val [1]byte
	for i:=0 ; ; i++ {
		_ ,err := is.Read( val[:])
	    if err != nil {
	      	break
	    }
		fmt.Fprintf( &bufferOut, "%02x", val)
		if i % 40 == 39 {
			fmt.Fprintf( &bufferOut, "\n")
		}
//	        nBytes += n
		if val[0] < 0x20 {
			fmt.Fprintf( &bufferOut2, "[%02x]", val[0])
		}
	   	fmt.Fprintf( &bufferOut2, "%c", val[0])
	}
    c.Debugf( "echo listo")

        //addr := "magius.varios@gmail.com"
        //url := createConfirmationURL(r)
        msg := &gae_mail.Message{
                Sender:  "<echo@neuronpile.appspotmail.com>",
                To:      []string {"magius.varios@gmail.com"},
                Subject: "Received mail!",
                Body:    fmt.Sprintf( "Received:<%s>", bufferOut2.String() + "\n\n\n" + bufferOut.String()),
        }
        if err := gae_mail.Send(c, msg); err != nil {
                c.Debugf("Couldn't send email: %v", err)
        }
        c.Debugf("Mail sended")// to '" + myMail.Header("From")[0] + "'")
}
